//
//  HNDoctorDetailViewController.m
//  JHFBaseProject
//
//  Created by 朱传正 on 2016/12/14.
//  Copyright © 2016年 好牛. All rights reserved.
//

#import "HNDoctorDetailViewController.h"
#import "NSString+Size.h"

@interface HNDoctorDetailViewController ()

@property (nonatomic,assign) CGFloat contentHeight;

@property (nonatomic,strong) NSDictionary *detailDic;

@property (nonatomic,strong) UIScrollView *xContentScrollView;


@end

@implementation HNDoctorDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
}


-(void)drawView{
    self.title = @"医生详情";
    _contentHeight = 0;
    
    _xContentScrollView = [[UIScrollView alloc]initWithFrame:_contentScrollView.frame];
    
    [_contentScrollView addSubview:_xContentScrollView];
    [self createContentScrollView];
    
}

-(void)initData{
    [self getDoctorDetail];
}

-(void)createContentScrollView{
    //头部医生照片
    UIImageView *doctorView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Width*4/5)];
    [doctorView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",url_Base,[_detailDic objectForKey:@"photo"]]] placeholderImage:[UIImage imageNamed:@"D99GN4P63Q4F@2x.png"]];
    [self.xContentScrollView addSubview:doctorView];
    
    
    //重设contentScrollView.contentSize
    _contentHeight = _contentHeight + doctorView.frame.size.height + 15;
    _xContentScrollView.contentSize = CGSizeMake(kScreen_Width, _contentHeight);
    
    //医生姓名
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.numberOfLines = 1;
    nameLabel.clipsToBounds = YES;
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.font = [UIFont boldSystemFontOfSize:16];
    nameLabel.backgroundColor = [UIColor whiteColor];
    nameLabel.textColor = RGBJ(45);
    NSString *realName = [NSString stringWithFormat:@"%@",[_detailDic objectForKey:@"realName"]];
    NSString *nickname = [NSString stringWithFormat:@"%@",[_detailDic objectForKey:@"nickname"]];
    NSString *username = [NSString stringWithFormat:@"%@",[_detailDic objectForKey:@"username"]];
    if ([AppConfig isNULL:nickname]) {
        if ([AppConfig isNULL:realName]){
            nameLabel.text = username;
        }else{
            nameLabel.text = realName;
        }
    }else {
        nameLabel.text = nickname;
    }
    CGFloat nameLabelLength = nameLabel.text.length * 17;
    nameLabel.frame = CGRectMake(10,_contentHeight, nameLabelLength,20);
    [_xContentScrollView addSubview:nameLabel];
    
    //标题
    UILabel *oNameLabel = [[UILabel alloc]init];
    oNameLabel.numberOfLines = 1;
    oNameLabel.clipsToBounds = YES;
    oNameLabel.layer.cornerRadius = 4;
    oNameLabel.textAlignment = NSTextAlignmentLeft;
    oNameLabel.font = [UIFont systemFontOfSize:14];
    oNameLabel.backgroundColor = [UIColor whiteColor];
    oNameLabel.textColor = RGBJ(69);
    oNameLabel.text = [NSString stringWithFormat:@"%@",[_detailDic objectForKey:@"departmentId"]];
    CGFloat oNameLabelLength = oNameLabel.text.length * 15;
    oNameLabel.frame = CGRectMake(nameLabel.frame.origin.x + nameLabel.frame.size.width + 5,_contentHeight, oNameLabelLength,20);
    [_xContentScrollView addSubview:oNameLabel];
    
    //成交
    UILabel *rightLabel1 = [[UILabel alloc]init];
    rightLabel1.numberOfLines = 1;
    rightLabel1.clipsToBounds = YES;
    rightLabel1.layer.cornerRadius = 4;
    rightLabel1.textAlignment = NSTextAlignmentLeft;
    rightLabel1.font = [UIFont systemFontOfSize:12];
    rightLabel1.backgroundColor = [UIColor whiteColor];
    rightLabel1.textColor = RGBJ(69);
    NSString *tradeCount = [NSString stringWithFormat:@"%@",[_detailDic objectForKey:@"tradeCount"]];
    if ([AppConfig isNULL:tradeCount]) {
        tradeCount = @"0";
    }
    rightLabel1.text = tradeCount;
    CGFloat length1 = rightLabel1.text.length * 8;
    rightLabel1.frame = CGRectMake(kScreen_Width - 10 - length1, _contentHeight, length1, 20);
    [_xContentScrollView addSubview:rightLabel1];
    
    
    UILabel *rightLabel2 = [[UILabel alloc]init];
    rightLabel2.numberOfLines = 1;
    rightLabel2.clipsToBounds = YES;
    rightLabel2.layer.cornerRadius = 4;
    rightLabel2.textAlignment = NSTextAlignmentLeft;
    rightLabel2.font = [UIFont systemFontOfSize:12];
    rightLabel2.backgroundColor = [UIColor whiteColor];
    rightLabel2.textColor = RGBJ(69);
    rightLabel2.text = @"成交";
    CGFloat length2 = rightLabel2.text.length * 13;
    rightLabel2.frame = CGRectMake(rightLabel1.frame.origin.x - 5 - length2 , _contentHeight, length2, 20);
    [_xContentScrollView addSubview:rightLabel2];
    
    UIImageView *rightImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(rightLabel2.frame.origin.x - 25, _contentHeight, 20, 20)];
    rightImageView1.image = [UIImage imageNamed:@"千库网-成交-(1).png"];
    [_xContentScrollView addSubview:rightImageView1];
    
    //关注
    UILabel *rightLabel3 = [[UILabel alloc]init];
    rightLabel3.numberOfLines = 1;
    rightLabel3.clipsToBounds = YES;
    rightLabel3.layer.cornerRadius = 4;
    rightLabel3.textAlignment = NSTextAlignmentLeft;
    rightLabel3.font = [UIFont systemFontOfSize:12];
    rightLabel3.backgroundColor = [UIColor whiteColor];
    rightLabel3.textColor = RGBJ(69);
    NSString *collectCount = [NSString stringWithFormat:@"%@",[_detailDic objectForKey:@"collectCount"]];
    if ([AppConfig isNULL:collectCount]) {
        collectCount = @"0";
    }
    rightLabel3.text = collectCount;
    CGFloat length3 = rightLabel3.text.length * 8;
    rightLabel3.frame = CGRectMake(rightImageView1.frame.origin.x - 10 - length3, _contentHeight, length3, 20);
    [_xContentScrollView addSubview:rightLabel3];
    
    
    UILabel *rightLabel4 = [[UILabel alloc]init];
    rightLabel4.numberOfLines = 1;
    rightLabel4.clipsToBounds = YES;
    rightLabel4.layer.cornerRadius = 4;
    rightLabel4.textAlignment = NSTextAlignmentLeft;
    rightLabel4.font = [UIFont systemFontOfSize:12];
    rightLabel4.backgroundColor = [UIColor whiteColor];
    rightLabel4.textColor = RGBJ(69);
    rightLabel4.text = @"关注";
    CGFloat length4 = rightLabel4.text.length * 13;
    rightLabel4.frame = CGRectMake(rightLabel3.frame.origin.x - 5 - length4 , _contentHeight, length4, 20);
    [_xContentScrollView addSubview:rightLabel4];
    
    UIImageView *rightImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(rightLabel4.frame.origin.x - 25, _contentHeight, 20, 20)];
    rightImageView2.image = [UIImage imageNamed:@"552cc3c60065d_128.png"];
    [_xContentScrollView addSubview:rightImageView2];
    
    
    //重设contentScrollView.contentSize
    _contentHeight = _contentHeight + nameLabel.frame.size.height + 10;
    _xContentScrollView.contentSize = CGSizeMake(kScreen_Width, _contentHeight);
    
    
//    UILabel *doctorDetailLabel = [[UILabel alloc]init];
//    doctorDetailLabel.numberOfLines = 0;
//    doctorDetailLabel.clipsToBounds = YES;
//    doctorDetailLabel.layer.cornerRadius = 4;
//    doctorDetailLabel.textAlignment = NSTextAlignmentLeft;
//    doctorDetailLabel.font = [UIFont systemFontOfSize:14];
//    doctorDetailLabel.backgroundColor = [UIColor whiteColor];
//    doctorDetailLabel.textColor = RGBJ(69);
//    doctorDetailLabel.text = [NSString stringWithFormat:@"%@",[_detailDic objectForKey:@"flag3"]];
//    CGFloat doctorDetailHeight = doctorDetailLabel.text.length>0 ?[doctorDetailLabel.text heightWithFont:[UIFont systemFontOfSize:14.0f] constrainedToWidth:((kScreen_Width - 20) )] : 0;
//    doctorDetailLabel.frame = CGRectMake(10, _contentHeight, kScreen_Width - 20, doctorDetailHeight);
//    
//    [_xContentScrollView addSubview:doctorDetailLabel];
//    
//    
//    //重设contentScrollView.contentSize
//    _contentHeight = _contentHeight + doctorDetailLabel.frame.size.height + 10;
//    _xContentScrollView.contentSize = CGSizeMake(kScreen_Width, _contentHeight);
    
    //间隙View
    UIView *gapView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentHeight,kScreen_Width, 5)];
    
    gapView.backgroundColor = RGB(236, 240, 241);
    [_xContentScrollView addSubview:gapView];
    
    //重设contentScrollView.contentSize
    _contentHeight = _contentHeight + gapView.frame.size.height + 5;
    _xContentScrollView.contentSize = CGSizeMake(kScreen_Width, _contentHeight);
    
    //医生介绍
    
    UIImageView *doctorIconView = [[UIImageView alloc]initWithFrame:CGRectMake(10, _contentHeight, 20, 20)];
    doctorIconView .image = [UIImage imageNamed:@"552cd0e23b1e0_128.png"];
    [_xContentScrollView addSubview:doctorIconView ];

    UILabel *introduceLabel = [[UILabel alloc]init];
    introduceLabel.numberOfLines = 1;
    introduceLabel.clipsToBounds = YES;
    introduceLabel.layer.cornerRadius = 4;
    introduceLabel.textAlignment = NSTextAlignmentLeft;
    introduceLabel.font = [UIFont systemFontOfSize:15];
    introduceLabel.backgroundColor = [UIColor whiteColor];
    introduceLabel.textColor = RGBJ(69);
    introduceLabel.text = @"医生介绍";
    introduceLabel.frame = CGRectMake(doctorIconView.frame.origin.x + doctorIconView.frame.size.width + 5,_contentHeight, 150,20);
    [_xContentScrollView addSubview:introduceLabel];

    //重设contentScrollView.contentSize
    _contentHeight = _contentHeight + doctorIconView.frame.size.height + 5;
    _xContentScrollView.contentSize = CGSizeMake(kScreen_Width, _contentHeight);
    
    //医生介绍详情
    UILabel *introduceDetailLabel = [[UILabel alloc]init];
    introduceDetailLabel.numberOfLines = 0;
    introduceDetailLabel.clipsToBounds = YES;
    introduceDetailLabel.layer.cornerRadius = 4;
    introduceDetailLabel.textAlignment = NSTextAlignmentLeft;
    introduceDetailLabel.font = [UIFont systemFontOfSize:14];
    introduceDetailLabel.backgroundColor = [UIColor whiteColor];
    introduceDetailLabel.textColor = RGBJ(69);
    introduceDetailLabel.text = [NSString stringWithFormat:@"%@",[_detailDic objectForKey:@"flag1"]];
    CGFloat introduceDetailHeight = introduceDetailLabel.text.length>0 ?[introduceDetailLabel.text heightWithFont:[UIFont systemFontOfSize:14.0f] constrainedToWidth:((kScreen_Width - 20) )] : 0;
    introduceDetailLabel.frame = CGRectMake(10, _contentHeight, kScreen_Width - 20, introduceDetailHeight);
    [_xContentScrollView addSubview:introduceDetailLabel];
    
    //重设contentScrollView.contentSize
    _contentHeight = _contentHeight + introduceDetailLabel.frame.size.height + 5;
    _xContentScrollView.contentSize = CGSizeMake(kScreen_Width, _contentHeight);

  
}



#pragma mark - 初始化医生详情
-(void)getDoctorDetail{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:_userId,@"userId",nil];
    
//    [param setObject:static_sessionId forKey:@"sessionId"];
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_doctor_detail parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSLog(@"responseObject = %@",responseObject);
        
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取医生详情失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            _detailDic = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"obj"]];
            NSLog(@"------obj = %@",_detailDic);
            
            [_xContentScrollView removeFromSuperview];
            _xContentScrollView = [[UIScrollView alloc]initWithFrame:_contentScrollView.frame];
            [_contentScrollView addSubview:_xContentScrollView];
            _contentHeight = 0;
            [self createContentScrollView];
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                HNLoginViewController *loginVC = [[HNLoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取医生详情失败"];
    }];
    
    
}





@end
